import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
 * Creates GUI to input the arguments that the Client requires to run.
 *
 * @author BluJaded Team
 * @version (see Sourceforge.net)
 */
public class ClientInputGui implements MouseListener, WindowListener {

    //the jframe of the GUI
    private JFrame frame;

    //textfield to input a port number
    private JTextField portT;

    //textfield to input a hostname or ip
    private JTextField hostT;

    //textfield to input a nickname
    private JTextField nickT;

    //Jbutton to accept and procces the input supplied
    private JButton acceptB;

    /**
     * Creates the Gui.
     */
    private ClientInputGui() {

        frame = new JFrame("New Client");
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new BorderLayout());
        Container inputPanel = new JPanel();
        inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
        hostT = new JTextField("localhost");
        portT = new JTextField("1352");
        nickT = new JTextField("");
        inputPanel.add(new JLabel(" "));
        inputPanel.add(new JLabel("Please specify a Hostname or IP for"));
        inputPanel.add(new JLabel(" a Jadetalk Server :"));
        inputPanel.add(new JLabel(" "));
        inputPanel.add(hostT);
        inputPanel.add(new JLabel(" "));
        inputPanel.add(new JLabel("Please specify the port the JadeTalk"));
        inputPanel.add(new JLabel("Server is listening on:"));
        inputPanel.add(new JLabel(" "));
        inputPanel.add(portT);
        inputPanel.add(new JLabel(" "));
        inputPanel.add(new JLabel("Please specify a Nickname to identify"));
        inputPanel.add(new JLabel("yourself on the Server:"));
        inputPanel.add(new JLabel(" "));
        inputPanel.add(nickT);
        inputPanel.add(new JLabel(" "));
        acceptB = new JButton("Accept");
        acceptB.addMouseListener(this);
        contentPane.add(acceptB, BorderLayout.SOUTH);
        contentPane.add(new JLabel("       "), BorderLayout.WEST);
        contentPane.add(new JLabel("       "), BorderLayout.EAST);
        contentPane.add(inputPanel, BorderLayout.CENTER);
        frame.addWindowListener(this);
        frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);
    }

    /**
     * mouseClicked Event occurs when the mouse if clicked on a listening object.
     *
     * @param      e    the mouseEvent object assosiated with the event.
     */
    public void mouseClicked(MouseEvent e) {

        //if the accept button was clicked
        if (e.getSource() == acceptB){

            //splits the inputted text into its composite words and adds these words to a string array
            //a word being anything surrounded by whitespaces
            //if the string begins with one or more whitespaces then the first element will hold an empty string
            String[] inpt = nickT.getText().split("\\s+");

            //if the inputed text was an empty string then inpt will be an empty array
            //if inpt is empty
            if (inpt.length==0){
                //makes inpt an array with a single empty string so that it can be proccessed below
                inpt = new String[]{""};
            }


            //if the first element of the inpt array is an empty string and there is more than one element in the array
            if (inpt[0].equals("") && inpt.length>1) {

                //creates a new string array with one less element than the inpt array
                String[] inptcopy= new String[inpt.length-1];

                //copies each but the first element of inpt into the new string array
                for(int i = 1;i < inpt.length;i++){
                    inptcopy[i - 1] = inpt[i];
                }

                //passes the new shortened array to the inpt array
                inpt=inptcopy;

            }

            //if the nickname field is not blank
            if (!inpt[0].equals("")){
                try {
                    //if the port number provided is between 1024 and 49151
                    int port = Integer.parseInt(portT.getText());
                    if (port>=1024 && port<=49151){
                        try {
                            //creates a client using the input provided and defines the UI as the GUI interface
                            Client.main(new String[]{hostT.getText(),portT.getText(),"true",inpt[0]});
                            //closes the window by disposing of the frame.
                            frame.dispose();
                        } 
                        catch (IOException ex) {
                            //if an error occurs prints the stack trace to assist debugging
                            //ex.printStackTrace();
                        }
                    }
                    else{
                        //display a message box to give the user feedback on their incorrect input
                        JOptionPane.showMessageDialog(frame,
                                "You must provide a valid and free port number between 1024 and 49151. ",
                                "Invalid Port Number",
                                JOptionPane.INFORMATION_MESSAGE);
                    }
                } 
                catch (NumberFormatException ex) {
                    //if the input in port field is not recognised as an integer
                    //display a message box to give the user feedback on their incorrect input
                    JOptionPane.showMessageDialog(frame,
                            "You must provide a valid and free port number between 1024 and 49151. ",
                            "Invalid Port Number",
                            JOptionPane.INFORMATION_MESSAGE);
                }

            }
            else{
                //display a message box to give the user feedback on their incorrect input
                JOptionPane.showMessageDialog(frame,
                        "You must provide a Nickname between 1 and 10 characters.\nNo Spaces Allowed.\nLonger Nicknames will be trimmed to length. ",
                        "Invalid Nickname",
                        JOptionPane.INFORMATION_MESSAGE);
                nickT.setText("");
            }
        }
    }

    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    public void mousePressed(MouseEvent e) {}

    public void mouseReleased(MouseEvent e)  {}

    public void windowActivated(WindowEvent e)  {}

    public void windowClosed(WindowEvent e)  {}

    /**
     * windowClosing Event occurs when the window is closed by the user or program.
     *
     * @param      e    the WindowEvent object assosiated with the event.
     **/
    public void windowClosing(WindowEvent e)  {

        //closes the window by disposing of the frame.
        frame.dispose();
        //exits any remaining proccessing a perfoms garbage collection on assosiated memory
        System.exit(0);

    }

    public void windowDeactivated(WindowEvent e)  {}

    public void windowDeiconified(WindowEvent e)  {}

    public void windowIconified(WindowEvent e)  {}

    public void windowOpened(WindowEvent e)  {}

    /**
     * main method provides an execution start and allows commandline execution.
     *
     * @param args The standard commandline arguments parameter.
     */
    public static void main(String[] args){
        new ClientInputGui();
    }


}
